package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsDao;
import com.cn.wanxi.model.DataField;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.GetSqlSession;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NewsDaoImpl implements INewsDao {

    private void clearNewsRedis(){
        try {
            Jedis jedis = new Jedis();
            jedis.del("news_id");
            jedis.del("news_author");
            jedis.del("news_intro");
            jedis.del("news_title");
            jedis.del("news_content");
            jedis.del("news_read_count");
            jedis.del("news_create_time");
            jedis.del("news_is_show");
            jedis.del("news_enabled");
            jedis.del("news_type");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public int addNews(NewsModel newsModel) {
        clearNewsRedis();
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.INewsDao.addNews",newsModel);
    }

    @Override
    public int deleteNews(int id) {
        clearNewsRedis();
        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.dao.INewsDao.deleteNews",id);
    }

    @Override
    public int editNews(NewsModel newsModel) {
        clearNewsRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsDao.editNews",newsModel);
    }

    @Override
    public List<NewsModel> getNewsList(Map<String,Object> map) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.getNewsList",map);
    }

    @Override
    public long getNewsListCount(Map<String, Object> map) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.getNewsListCount",map);
    }

    @Override
    public NewsModel getNews(int id) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.getNews",id);
    }

    @Override
    public List<NewsModel> getAllNews(Map<String,Object> map) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.getAllNews",map);
    }

    @Override
    public long getNewsCount(Map<String, Object> map) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.getNewsCount",map);
    }


    @Override
    public int updateShow(NewsModel news) {
        clearNewsRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsDao.updateShow",news);
    }

    @Override
    public List<NewsModel> getNewsListByRedis() {
        List<NewsModel> newsList = new ArrayList<>();
        Jedis jedis = new Jedis();
        List<String> newsId = jedis.lrange("news_id",0,-1);
        for(int i=0;i<newsId.size();i++){
            NewsModel newsModel = new NewsModel();
            newsModel.setId(Integer.valueOf(newsId.get(i)));
            newsModel.setTitle(jedis.lindex("news_title",i));
            newsModel.setAuthor(jedis.lindex("news_author",i));
            newsModel.setIntro(jedis.lindex("news_intro",i));
            newsModel.setContent(jedis.lindex("news_content",i));
            newsModel.setIsShow(Integer.parseInt(jedis.lindex("news_is_show",i)));
            newsModel.setEnabled(Integer.parseInt(jedis.lindex("news_enabled",i)));
            newsModel.setCreate_time(jedis.lindex("news_create_time",i));
            newsModel.setReadCount(Integer.parseInt(jedis.lindex("news_read_count",i)));
            newsModel.setNewsType(new NewsTypeDaoImpl().getNewsType(Integer.parseInt(jedis.lindex("news_type",i))));
            newsList.add(newsModel);
        }
        return newsList;
    }

    @Override
    public void setNewsListInRedis(List<NewsModel> newsList) {
        Jedis jedis = new Jedis();
        //只缓存第一页的
        for (NewsModel newsModel:newsList) {
            jedis.rpush("news_id", String.valueOf(newsModel.getId()));
            jedis.rpush("news_title",newsModel.getTitle());
            jedis.rpush("news_author",newsModel.getAuthor());
            jedis.rpush("news_intro",newsModel.getIntro());
            jedis.rpush("news_content",newsModel.getContent());
            jedis.rpush("news_create_time",newsModel.getCreate_time());
            jedis.rpush("news_read_count", String.valueOf(newsModel.getReadCount()));
            jedis.rpush("news_is_show", String.valueOf(newsModel.getIsShow()));
            jedis.rpush("news_enabled", String.valueOf(newsModel.getEnabled()));
            jedis.rpush("news_type", String.valueOf(newsModel.getNewsType().getId()));
        }
    }
}
